<?php

namespace app\user\controller;

use think\Controller;
use think\Request;
use think\Lang;


class base extends Controller
{

    protected $system, $model;

    public function _initialize()
    {
       $this->model = model($this->request->controller());
    }


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (!$this->getLoginUser()) {
            if($this->request->isAjax()){
                return false;
            }
            return $this->redirect('Login/index');
        }
        $this->assign('user', $this->getLoginUser());
        $this->_html_empty();
        //头部导航
        $datas['status'] = '1';
        $datas['lang'] = cookie('think_var') ? cookie('think_var') : 'zh-cn';
        $navs = model('nav')->catenav($datas);

        $json_navs = json_encode($navs);

        //底部导航
        $nav = cache('nav'. cookie('think_var'));
        if (empty($nav)) {
            $nav = model('nav')
                ->where('pid', '0')
                ->where('status', '1')
                ->where('lang', cookie('think_var'))
                ->order('sort ASC,id ASC')
                ->limit(10)->select();
            cache('nav'. cookie('think_var'), $nav);
        }

        //在线帮助
        $online = cache('online'. cookie('think_var'));
        if (empty($online)) {
            $online = model('online')->select();
            cache('online'. cookie('think_var'), $online);
        }
        //全站信息
//        $this->system = model('system')->getnoteall('system');

        $this->assign([
            'online' => $online,
            'json_navs' => $json_navs,
            'nav' => $nav,
            'empty' => Lang::get('empty'),
            'system' => cache('systemsystem'. cookie('think_var')),
        ]);
    }


    public function index()
    {
        $list = $this->model->page_index($this->datas, 'create_time desc,id desc')
            ->appends($this->request->param());
        $this->assign([
            'list' => $list,
            'num' => $this->model->where($this->datas)->count(),
        ]);
        return $this->fetch();
    }


    public function getLoginUser()
    {
        $Admin = session('User', '', 'User');
        if ($Admin && $Admin->id) {
            return model('User')->get(['id' => $Admin->id]);
        }
        return false;
    }


    /**
     * 更新
     * @param $data
     * @return \think\response\Json
     */
    public function update($data){
        $res = $this->model->allowField(true)->save($data, ['id' => intval($data['id'])]);
        if ($res) {
            return ajax_info(lang('Successfully modified'),'u');
        } else {
            return ajax_info(lang('No change'));
        }
    }

    /**
     *
     * 删除
     * @return \think\response\Json
     */
    public function del($id)
    {
        if ($this->request->isAjax()) {
            $res = $this->model->del($id);
            if ($res) {
                ajaxMsg('1', lang('successfully deleted'));
                return $this->success(lang('successfully deleted'));
            } else {
                ajaxMsg('0', lang('Not deleted'));
                return $this->error(lang('Not deleted'));
            }
        }
    }

    public function _html_empty()
    {
        $this->assign('_html_empty', '<tr><td colspan=100>'.lang('Empty').'</td></tr>');
    }

    //退出登陆
    public function logout()
    {
        session(null, 'User');
        $this->redirect('Login/index');
    }

}
